import { QFrame_App } from "../../ttapi_layer2/ttlayer2.js";
import { UserData, UserState } from "../game.js";
import * as tt2 from "../../ttapi_layer2/ttlayer2.js"
import { tt } from "../../ttapi_interface/ttapi.js";
import { State_CardInfo } from "./state_cardinfo.js";
import { State_Game_Endless } from "./state_game_endless.js";
import { State_BattleChoose } from "./state_battlechoose.js";

export class State_MainMenu extends UserState {
    override OnInit(statemgr: QFrame_App<UserData>): void {
        super.OnInit(statemgr);
        this.InitAsync();
    }
    override  OnExit(): void {
        let _ui = this.userdata.scene.GetUI();
        _ui.removeChildAll();
    }
    private _inited: boolean = false;
    async InitAsync(): Promise<void> {
        let _ui = this.userdata.scene.GetUI();
        let _uiMidY = _ui.getWorldRect().Height / 2 - 20;


        {
            let _head = await tt2.QFrame_ResMgr.LoadStandaloneSpriteAsync("header.png");
            let _imghead = new tt2.QUI_Image(_head);
            _imghead.localRect.setHPosByCenter(240);
            _imghead.localRect.setVPosByTopBorder(_head.totalHeight, 0);
            _ui.addChild(_imghead);
        }
        {
            let _foot = await tt2.QFrame_ResMgr.LoadStandaloneSpriteAsync("footer.png");
            let _imgfoot = new tt2.QUI_Image(_foot);
            _imgfoot.localRect.setHPosByCenter(240);
            _imgfoot.localRect.setVPosByBottomBorder(_foot.totalHeight, 0);
            _ui.addChild(_imgfoot);
        }

        {
            let _btn1 = await this.CreateBtn("战役模式");
            _btn1.localRect.setVPosByTopBorder(40, _uiMidY - 50);
            _ui.addChild(_btn1);
            _btn1.OnClick = () => {
                this.app.ChangeState(new State_BattleChoose(null));
            }
        }
        {
            let _btn2 = await this.CreateBtn("无尽模式");
            _btn2.localRect.setVPosByTopBorder(40, _uiMidY + 0);
            _ui.addChild(_btn2);
            _btn2.OnClick = () => {
                this.app.ChangeState(new State_Game_Endless());
            }
        }
        {
            let _btn3 = await this.CreateBtn("卡片情报");
            _btn3.localRect.setVPosByTopBorder(40, _uiMidY + 50);
            _ui.addChild(_btn3);
            _btn3.OnClick = () => {
                this.app.ChangeState(new State_CardInfo());
            }
        }

        this._inited = true;
    }
    async CreateBtn(text: string): Promise<tt2.QUI_Button> {
        let _btnborder = await tt2.QFrame_ResMgr.LoadStandaloneSpriteAsync("btnborder.png");
        let btn = new tt2.QUI_Button();
        btn.localRect.setHPosByCenter(160);
        btn.localRect.setVPosByBottomBorder(40, 30);
        let bimg: tt2.QUI_ImageScale9;
        {
            let s9 = new tt2.QUI_Scale9(_btnborder, new tt.Border(4, 4, 4, 4));
            let label = new tt2.QUI_Label(this.app.font, text);
            label.fontScale.X = 2;
            label.fontScale.Y = 2;
            label.color = new tt.Color(0.3, 0.3, 0.3, 1);
            bimg = new tt2.QUI_ImageScale9(s9);
            label.localRect.setAsFill();
            bimg.addChild(label);
            bimg.localRect.setAsFill();
        }
        let bimg2 = bimg.Clone() as tt2.QUI_ImageScale9;
        bimg2.color = new tt.Color(0.5, 0.5, 0.5, 1.0);

        //bimg2.localRect.setAsFill();
        btn.ElemNormal = bimg;
        btn.ElemPress = bimg2;
        return btn;
    }
}